package hashmap;

public class MyHashMap {
	Node[] table;
	int size;

	public MyHashMap() {
		table = new Node[16];
	}

	public void put(Object key, Object value) {
		Node newNode = new Node();
		newNode.hash = myhash(key.hashCode(), table.length);
		newNode.key = key;
		newNode.value = value;
		newNode.next = null;
		Node temp = table[newNode.hash];
		Node last = null;
		boolean keyRepeat = false;
		if (temp == null) {
			table[newNode.hash] = newNode;
		} else {
			while (temp != null) {
				if (temp.key.equals(key)) {
					temp.value = value;
					keyRepeat = true;
					break;
				} else {
					last = temp;
					temp = temp.next;
				}
			}
			if (!keyRepeat)
				last.next = newNode;
		}
	}

	private int myhash(int v, int length) {
		return v & (length - 1);

	}

	public static void main(String[] args) {
		MyHashMap mh = new MyHashMap();
		mh.put(10, "a");
		mh.put(26, "26");
		mh.put(20, "aa");
		mh.put(30, "aaa");
		mh.put(10, "aaaa");
		System.out.println(mh);
	}
}

class Node {
	int hash;
	Object key;
	Object value;
	Node next;
}